<?php

/**
 * This is the model class for table "tb_monitoring".
 *
 * The followings are the available columns in table 'tb_monitoring':
 * @property string $id
 * @property integer $tb_kartu_id
 * @property integer $tb_mobil_id
 * @property string $noDokumen
 * @property string $timbanganMasuk
 * @property string $timbanganKeluar
 * @property integer $statusParkir
 * @property string $created
 * @property integer $createdBy
 */
class Monitoring extends CActiveRecord
{

        
        private $cacheId;
    /**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tb_monitoring';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('tb_mobil_id, noDokumen', 'required'),
			array('tb_mobil_id', 'numerical', 'integerOnly'=>true),
			array('noDokumen', 'length', 'max'=>20),
			array('noTiket, noDokumen', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('noDokumen', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    'Kartu' => array(self::BELONGS_TO, 'Kartu', 'tb_kartu_id'),
                    'Mobil' => array(self::BELONGS_TO, 'Mobil', 'tb_mobil_id'),
                    'BongkarMuat' => array(self::HAS_ONE, 'BongkarMuat', 'tb_monitoring_id'),
                    'Notes' => array(self::HAS_MANY, 'Notes', 'tb_monitoring_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'tb_kartu_id' => Yii::t('model', 'No Tiket'),
			'tb_mobil_id' => Yii::t('model', 'No Polisi'),
			'noDokumen' => Yii::t('model', 'No Dokumen'),
			'timbanganMasuk' => Yii::t('model', 'Timbangan Masuk'),
			'timbanganKeluar' => Yii::t('model', 'Timbangan Keluar'),
			'statusParkir' => Yii::t('model', 'Status Parkir'),
			'created' => Yii::t('model', 'Created'),
			'createdBy' => Yii::t('model', 'Created By'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;
                
		$criteria->compare('Kartu.noTiket',$this->tb_kartu_id,true);
                $criteria->compare('Mobil.noPolisi',$this->tb_mobil_id,true);
                $criteria->compare('d.noDokumen',$this->noDokumen,true);
                $criteria->join = 'LEFT JOIN tb_mobil Mobil ON (t.tb_mobil_id=Mobil.id) 
                                   LEFT JOIN tb_kartu Kartu ON (t.tb_kartu_id=Kartu.id)
                                   LEFT JOIN tb_bongkar_muat BongkarMuat ON (t.id=BongkarMuat.tb_monitoring_id)
                                   LEFT JOIN ref_gudang Gudang ON (BongkarMuat.ref_gudang_id=Gudang.id)';
                $criteria->order = 't.id DESC';
                
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Monitoring the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
        
        public function beforeSave()
        {
            if($this->isNewRecord)
            {
                $this->created = new CDbExpression('NOW()');
                $this->createdBy = Yii::app()->user->id;
            }
            return parent::beforeSave();
        }

        public function beforeDelete()
        {
                $this->cacheId = $this->id;

                return parent::beforeDelete();
        }

        public function afterDelete()
        {
                $bongkarMuats = BongkarMuat::model()->findAll(
                        'tb_monitoring_id=:id',
                        array(
                                ':id'=>$this->cacheId
                        )
                );
                $notes = Notes::model()->findAll(
                            'tb_monitoring_id=:id',
                            array(
                                    ':id'=>$this->cacheId
                            )
                        );
                if(!empty($bongkarMuats))
                {
                    foreach($bongkarMuats as $bongkarMuat)
                    {
                            $ekspedisis = Ekspedisi::model()->findAll(
                                    'tb_bongkar_muat_id=:id',
                                    array(
                                        ':id'=>$bongkarMuat->id,
                                    )
                                );
                            if(!empty($ekspedisis))
                            {
                                foreach ($ekspedisis as $ekspedisi) {
                                    $ekspedisi->delete();
                                }
                            }
                            $bongkarMuat->delete();
                    }
                }
                if(!empty($notes))
                {
                    foreach ($notes as $note) {
                        $note->delete();
                    }
                }
                parent::afterDelete();
        }
}
